<?php

namespace App\Hospital\Metrics\Examples;

use App\Models\HospitalUserFamily;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Donut;

class SignProportion extends Donut
{
    protected $labels = ["0-3岁",'3-6岁'];

    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $color = Admin::color();
        $colors = [$color->primary(), $color->alpha('blue2', 0.5)];

        $this->title('签约儿童年龄比例');
        //$this->subTitle('Last 30 days');
        $this->chartLabels($this->labels);
        // 设置图表颜色
        $this->chartColors($colors);
    }

    /**
     * 渲染模板
     *
     * @return string
     */
    public function render()
    {
        $this->fill();

        return parent::render();
    }

    /**
     * 写入数据.
     *
     * @return void
     */
    public function fill()
    {
        $sign1 = HospitalUserFamily::query()->where('eb_hospital_user_family.hospital_id',Admin::user()->hospital_id)
            ->join('eb_family', "eb_hospital_user_family.family_id", '=', "eb_family.id")
            ->where('eb_hospital_user_family.status',1)
            ->where('eb_hospital_user_family.type',1)
            ->where('eb_family.birthday','>=',date('Y-m-d',strtotime("-3 year")))
            ->count();
        $sign2 = HospitalUserFamily::query()->where('eb_hospital_user_family.hospital_id',Admin::user()->hospital_id)
            ->join('eb_family', "eb_hospital_user_family.family_id", '=', "eb_family.id")
            ->where('eb_hospital_user_family.status',1)
            ->where('eb_hospital_user_family.type',1)
            ->where('eb_family.birthday','<=',date('Y-m-d',strtotime("-3 year")))
            ->where('eb_family.birthday','>=',date('Y-m-d',strtotime("-7 year")))

            ->count();
        $this->withContent($sign1, $sign2);

        // 图表数据
        $this->withChart([$sign1, $sign2]);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data
        ]);
    }

    /**
     * 设置卡片头部内容.
     *
     * @param mixed $desktop
     * @param mixed $mobile
     *
     * @return $this
     */
    protected function withContent($sign1, $sign2)
    {
        $blue = Admin::color()->alpha('blue2', 0.5);

        $style = 'margin-bottom: 8px';
        $labelWidth = 120;

        return $this->content(
            <<<HTML
<div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
    </div>
    <div>{$sign1}</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
    </div>
    <div>{$sign2}</div>
</div>
HTML
        );
    }
}
